# -*- coding: utf-8 -*-
'''
Created on 30/ott/2009

@author: sax
'''
from django.core.urlresolvers import reverse
from xadmin.admin import XModelAdmin
from xadmin.tests.modelprovider import ModelProviderTest, TestMasterModel
from django.contrib.auth.models import User, Permission
from django.test.client import Client        
from django.contrib.contenttypes.models import ContentType 
from xadmin.xbuttons import XModelAdminButton




class TestXModelAdmin(ModelProviderTest, AdminModelManager):
    SUPERUSER="thisIsATestSuperUser"
    USER="thisIsATestStandardUser"
    
    CURRENT_USER = SUPERUSER
        
    def setUp(self):
        super(TestXModelAdmin, self).setUp()
        self._init()
        a,x=User.objects.get_or_create(username=self.SUPERUSER, password="123", email="a@b.com", is_staff=True, is_active=True, is_superuser=True)
        b,x=User.objects.get_or_create(username=self.USER, password="123", email="b@b.com", is_staff=True, is_active=True, is_superuser=False)
        a.set_password("123")
        b.set_password("123")
        a.save()
        b.save()
        
        self.set_urls()
        self._login()
        
    def _login(self):       
        from django.contrib.auth import authenticate, login
        #self.client.logout()
        self.client = Client()
        self.user = authenticate( username=self.CURRENT_USER, password="123" ) 
        logged = self.client.login(username=self.CURRENT_USER, password="123")
        self.assertTrue( logged ) 
        ret = self.client.get("/jaas/")
        self.assertContains(ret, "logout", 1, 200)
                
    def tearDown(self):
        self._register(TestMasterModel, XModelAdmin)
            
    def _test_generic_view(self, action, _args=None, retcode=None):
        m = self.admin_site._registry[TestMasterModel]
        cv = m._get_view_name(action)
        url = reverse("aa:"+cv, args=_args)
        ret = self.client.get(url)

        if retcode:
            self.assertEqual(ret.status_code, retcode)
        return ret
    
    def test_view_changelist(self):
        ret =  self._test_generic_view("changelist", None, 200)
        self.assertTrue(ret.context['has_read_permission'])
        return ret
    
    def test_view_change(self):
        TestMasterModel.objects.get_or_create(name="test")
        ret =   self._test_generic_view("change", [1], 200)
        self.assertTrue(ret.context['has_change_permission'])
        return ret
        
    def test_view_add(self):
        ret =  self._test_generic_view("add", None,  200)
        self.assertTrue(ret.context['has_add_permission'])
        return ret

    def test_view_delete(self):
        model, isnew = TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("delete", [1], 200)
        self.assertEqual(model, ret.context['object'])
        return ret

    def test_view_read(self):
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("read", [1], 200)
        self.assertTrue(ret.context['has_read_permission'])
        return ret

    def test_view_history(self):
        TestMasterModel.objects.get_or_create(name="test")
        ret =  self._test_generic_view("history", [1], 200)
        return ret

    def test_extra_meta_methods(self):
        m, isnew = TestMasterModel.objects.get_or_create(name="test")
        assert hasattr(m._meta,'get_read_permission')
        assert hasattr(m._meta,'get_read_field_restriction')
        assert hasattr(m._meta,'get_edit_field_restriction')

    def test_for_extra_permission(self):
        m, isnew = TestMasterModel.objects.get_or_create(name="test")
        m = self.admin_site._registry[TestMasterModel]
        from django.http import HttpRequest         
        r = HttpRequest() 
        r.user = self.user
        m.has_read_permission( r )

class TestVerboseName(TestXModelAdmin):
    class T(XModelAdmin):
        verbose_name_lural = "VERBOSE"

    ADMIN_CLASS = T
        
    def test_set_verbose_name_plural(self):
        m, isnew = TestMasterModel.objects.get_or_create(name="test")        

class TestReadOnly(TestXModelAdmin):
    class T(XModelAdmin):
        read_only = True

    ADMIN_CLASS = T

    def test_view_delete(self):
        "cannot delete read_only"
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("delete", [1])
        self.assertEqual(ret.status_code, 403)

    def test_view_change(self):
        "cannot change read_only"
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("change", [1])
        self.assertEqual(ret.status_code, 200)
        self.assertFalse(ret.context['change'])
        self.assertTrue(ret.context['read_only'])

    def test_view_read(self):
        "cannot change read_only"
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("change", [1])
        self.assertEqual(ret.status_code, 200)

class TestNoPermissions(TestXModelAdmin):       
    CURRENT_USER = TestXModelAdmin.USER 
    
    def _login(self):
        ct = ContentType.objects.get_for_model(TestMasterModel)
        perms = Permission.objects.filter(content_type=ct)
        
        # remove all permission to a user        
        self.user = User.objects.get(username=self.CURRENT_USER)
        self.user.user_permissions.remove(*perms)
        self.user.save()

        super(TestNoPermissions, self)._login()
                
    def test_view_delete(self):
        "cannot delete read_only"        
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("delete", [1])
        self.assertEqual(ret.status_code, 403)

    def test_view_change(self):
        "cannot chqange read_only"        
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("change", [1])
        self.assertEqual(ret.status_code, 403)

    def test_view_read(self):
        "cannot chqange read_only"        
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("read", [1])
        self.assertEqual(ret.status_code, 403)

    def test_view_changelist(self):
        "cannot chqange read_only"        
        ret =  self._test_generic_view("changelist", None, 403)
        #self.assertEqual(ret.status_code, 403)

    def test_view_add(self):
        "cannot chqange read_only"        
        ret =  self._test_generic_view("add", None, 403)

    def test_view_history(self):
        "cannot chqange read_only"        
        TestMasterModel.objects.get_or_create(name="test") #@UndefinedVariable
        ret =  self._test_generic_view("history", [1], 403)

class TestFeatureButtons(TestXModelAdmin):
    class T(XModelAdmin):
        buttons = [XModelAdminButton("label", "link")]

    ADMIN_CLASS = T
    
    
    
    
    